﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace newTurtle8
{
    public delegate void DelegateHandler(List<string> armyIdList);
    abstract public class Mofa
    {
        public delegate void ZimunimListChangedHandler();
        public event ZimunimListChangedHandler ZimunimList_Modified;

        #region properties
        protected DateTime startDate;
        protected DateTime endDate;

        public readonly string TableName;
        public readonly string ZimunimTableName;
        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }

        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }
        #endregion

        public Mofa(DateTime start, DateTime end, string tableName, string zimunimTableName, bool isCreation)
        {
            startDate = start;
            endDate = end;
            this.TableName = tableName;
            this.ZimunimTableName = zimunimTableName;
            
            //adds all active soldiers to this Mofa, only if this is the first creation of this mofa.
            if (isCreation)
            {
                SqlCommand selectActiveSoldiersCmd = SqlManager.getSqlCommandFromFile(@"\SELECT\AllActiveSoldiers.SELECT.sql");
                DataTable dt = SqlManager.selectSqlQueryDataTable(selectActiveSoldiersCmd);
                List<string> list = new List<string>();

                foreach (DataRow row in dt.Rows)
                    list.Add((string)row["armyID"]);

                addSoldier(list);
            }
        }

        #region abstract methods
        abstract public void addSoldier(List<string> armyIdList);
        abstract public void removeSoldier(List<string> armyIdList);
       
        #endregion

        public void addYamams(string armyID, List<DateTime> dateList)
        {
            SqlCommand insertYamamCmd = SqlManager.getSqlCommandFromFile(@"\INSERT\Yamam.INSERT.sql");
            insertYamamCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = startDate;
            insertYamamCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = armyID;
            insertYamamCmd.Parameters.Add("@date", SqlDbType.Date);

            foreach (DateTime d in dateList)
            {
                insertYamamCmd.Parameters["@date"].Value = d;
                SqlManager.executeNonQuery(insertYamamCmd);
            }
        }
        public void deleteYamams(string armyID, List<DateTime> dateList)
        {
            SqlCommand deleteYamamCmd = SqlManager.getSqlCommandFromFile(@"\DELETE\Yamam.DELETE.sql");
            deleteYamamCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = startDate;
            deleteYamamCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = armyID;
            deleteYamamCmd.Parameters.Add("@date", SqlDbType.Date);

            foreach (DateTime d in dateList)
            {
                deleteYamamCmd.Parameters["@date"].Value = d;
                SqlManager.executeNonQuery(deleteYamamCmd);
            }
        }

        public int getTotalDays()
        {
            return (int)(endDate.Subtract(StartDate).Days) + 1;
        }

        protected void ZimunumListModified_Fire()
        {
            ZimunimList_Modified();
        }
    }
}
